package com.yhk.usercenter.controller;

import com.yhk.usercenter.common.BaseResponse;
import com.yhk.usercenter.common.ErrorCode;
import com.yhk.usercenter.common.ResultUtil;
import com.yhk.usercenter.exception.BusinessException;
import com.yhk.usercenter.model.domain.User;
import com.yhk.usercenter.model.request.UserLoginRequest;
import com.yhk.usercenter.model.request.UserRegisterRequest;
import com.yhk.usercenter.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

import static com.yhk.usercenter.constant.UserConstant.ADMIN_ROLE;
import static com.yhk.usercenter.constant.UserConstant.USER_LOGIN_STATUS;

/**
 * @version 1.0
 * @Author moresuo
 * @Date 2023/12/5 18:48
 * @注释 用户接口
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;
    @PostMapping("/register")
    public BaseResponse<Long> userRegister(@RequestBody UserRegisterRequest userRegisterRequest){
        if(userRegisterRequest==null){
            throw new BusinessException(ErrorCode.PARAMS_NULL_ERROE);//抛出异常
        }
        String userAccount = userRegisterRequest.getUserAccount();
        String userPassword = userRegisterRequest.getUserPassword();
        String checkPassword = userRegisterRequest.getCheckPassword();
        String planetCode= userRegisterRequest.getPlanetCode();
        if (StringUtils.isAnyBlank(userAccount, userPassword, checkPassword,planetCode)) {
            throw new BusinessException(ErrorCode.PARAMS_NULL_ERROE);
        }
        long result = userService.userRegister(userAccount, userPassword, checkPassword, planetCode);
        return ResultUtil.success(result);
    }

    @PostMapping("/login")
    public BaseResponse<User> userRegister(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request){
        if(userLoginRequest==null){
            throw new BusinessException(ErrorCode.PARAMS_NULL_ERROE);
        }
        String userAccount = userLoginRequest.getUserAccount();
        String userPassword = userLoginRequest.getUserPassword();
        if (StringUtils.isAnyBlank(userAccount, userPassword)) {
            throw new BusinessException(ErrorCode.PARAMS_NULL_ERROE);
        }
        User user = userService.userLogin(userAccount, userPassword, request);
        return ResultUtil.success(user);
    }
    @PostMapping("/logout")
    public BaseResponse<Integer> userLogOut(HttpServletRequest request){
        if(request==null){
            throw new BusinessException(ErrorCode.PARAMS_NULL_ERROE);
        }
        int result = userService.userLogout(request);
        return ResultUtil.success(result);
    }

    @GetMapping("/current")
    public BaseResponse<User> getCurrentUser(HttpServletRequest request){
        Object userObj = request.getSession().getAttribute(USER_LOGIN_STATUS);
        User userCurrent= (User) userObj;
        if(userCurrent==null){
            throw new BusinessException(ErrorCode.NOT_LOGIN);
        }
        //根据用户id去查询用户数据库的具体信息
        long userId = userCurrent.getId();
        //todo 校验用户是否合法
        User user = userService.getById(userId);
        User result = userService.getSafeUser(user);
        return ResultUtil.success(result);
    }

   @GetMapping("/search")
    public BaseResponse<List<User>> searchUsers(HttpServletRequest request){
        //鉴权，仅管理员可以查询
       if(!isAdmin(request)){
           throw new BusinessException(ErrorCode.NO_AUTH);
       }
       List<User> result = userService.searchUsers();
       return ResultUtil.success(result);
   }

    @PostMapping("delete")
    public BaseResponse<Boolean> deleteUser(@RequestBody long id,HttpServletRequest request){
        if(!isAdmin(request)){
            throw new BusinessException(ErrorCode.NO_AUTH);
        }
        if(id<=0){
            throw new BusinessException(ErrorCode.PARAMS_ERROE);
        }
        boolean result = userService.removeById(id);
        return ResultUtil.success(result);
    }

    /**
     * 是否为管理员
     * @param request
     * @return
     */
    private boolean isAdmin(HttpServletRequest request){
        //鉴权，仅管理员可以查询
        Object userObj = request.getSession().getAttribute(USER_LOGIN_STATUS);
        User user = (User) userObj;
        if(user==null||user.getUserRole()!=ADMIN_ROLE){
            return false;
        }
        return true;
    }
}
